# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 波士顿房价预测（2正好拟合）.py
# @Author: dongguangwen
# @Date  : 2025-02-06 11:31
# 0.导入工具包
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

# 1.准备数据
np.random.seed(22)
x = np.random.uniform(-3, 3, size=100)
# print(x)
y = 0.5 * x ** 2 + np.random.normal(0, 1, size=100)
print(y)

# 2.模型训练
model = LinearRegression()  # 实例化线性回归模型
X = x.reshape(-1, 1)  # 线性回归模型需要二维数组
X2 = np.hstack([X, X ** 2])  # 数据增加二次项
model.fit(X2, y)

# 3.模型预测
y_pred = model.predict(X2)
print(y_pred)

# 4.计算均方误差
myret = mean_squared_error(y, y_pred)
print('myret-->', myret)

# 5.展示效果
plt.scatter(X, y)
# 画图plot折线图时 需要对x进行排序, 取x排序后对应的y值
plt.plot(np.sort(x), y_pred[np.argsort(x)], color='red')
plt.show()

"""
[ 2.68991269 -0.15896455 -1.0657469   1.77691179  2.2169277   1.44760438
  1.95468165  1.43909861  0.15853376  1.32780734  4.86511909  0.39702949
  2.63475246 -0.14604323  2.36460552  1.62061024  0.83996403  3.84213349
  0.38401779  1.60688722  1.07599777  0.65478898  0.9188096  -0.64424027
  1.59171645 -0.51865919  1.34359201  2.92620657  2.80123304  4.40659537
  0.87811453  1.10113724  6.1923925   0.855368    2.53145803  2.20814387
  0.50914933  3.56598319 -1.52802892 -0.22965108  0.68003709  4.97404497
 -0.75143267  2.88409744  5.60301458  0.99887944  2.62802974  2.46495793
  3.66571442 -1.85804906  0.83140672  0.86165142  0.33581663  0.06046012
  2.05133222  0.89686498  0.570795    4.81741428 -0.24435866  1.68542272
  2.49369326  2.83636823 -0.23086228  2.26822132  2.33381088  0.67749307
  1.59425991  2.65328755  3.70118035  3.9285876   1.95288399  2.60958529
  3.87293405  3.76537917  0.40451931  3.28686949  0.78030988  4.27240723
  2.19597273 -0.5814483   1.44486224  0.43852349 -0.27671756  2.04600828
  4.32094833 -0.20276307  2.15265487  1.69291982  0.10829686  5.84398733
  0.48910081  5.08461625  0.35116797  2.44363252  0.07344753  1.08868863
  1.41343863  2.96338708  4.35563861  1.45501813]
[1.90508074 0.20308072 0.35238147 2.2385963  2.34427947 0.76013542
 1.2843344  0.70328292 1.77494347 1.70533403 4.80360575 0.208677
 1.72393649 1.08676072 2.1267162  4.88368209 1.31686833 3.60942366
 0.77218    1.05691505 1.28061398 0.68612508 0.49011897 0.34125454
 0.32846346 0.25260723 0.77673549 3.14435686 3.08959321 4.11288157
 0.62306964 1.53500187 4.47254193 2.26297566 2.43408325 1.08540552
 0.17988821 2.90986563 0.19968784 0.17781885 0.95178513 4.18694277
 0.29705111 3.38201896 4.19458639 0.66589975 2.11914512 2.95372353
 2.89967487 0.21604523 2.02831347 1.40492332 0.31569435 0.28065451
 3.998748   0.81879589 1.79287682 2.39022023 0.18422236 0.93583325
 2.92282592 3.52936074 0.93103307 1.78850831 1.02005965 1.88355866
 1.08244748 2.26568658 2.22976248 4.8952122  0.64528711 3.53647619
 3.36263138 4.57876755 0.59862202 2.43284246 0.21986224 1.55136414
 2.51297972 0.18209487 0.22646804 0.22136941 0.17985698 0.8617055
 2.5833797  1.45683079 2.27400628 0.41674821 0.74184914 4.44845154
 0.51689606 4.28980842 0.18055882 3.22639214 0.28810371 2.00001987
 1.62191216 1.66095984 3.98162335 1.48757753]
myret--> 0.9687659188513673
"""
